package com.system.userserver.service.imp;

import com.system.commonutils.bean.SecendMenu;
import com.system.commonutils.pojo.Menu;
import com.system.commonutils.pojo.Role;
import com.system.userserver.bean.MenuParams;
import com.system.userserver.bean.RoleInfo;
import com.system.userserver.bean.RoleMenusParams;
import com.system.userserver.bean.RoleParams;
import com.system.userserver.mapper.RoleMapper;
import com.system.userserver.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImp implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Override
    public List<Role> selectRoleList() {
        List<Role> roles = roleMapper.selectRoleList();
        return roles;
    }


    @Override
    public List<SecendMenu> getMenuByRole(Integer roleId) {
        List<SecendMenu> menus = roleMapper.selectMenuByRoleId(roleId);
        return menus;
    }

    @Override
    public RoleInfo selectRoleById(Integer roleId) {
        RoleInfo roleInfo = roleMapper.selectRoleById(roleId);
        return roleInfo;
    }

    @Override
    public Boolean addMenu(MenuParams menuParams) {
        Integer i = roleMapper.addMenu(menuParams);
        return i==1;
    }

    @Transactional
    @Override
    public Boolean addMenus(List<MenuParams> menuParams) {
        if(menuParams!=null&&menuParams.size()!=0){
            for (MenuParams menuParam : menuParams) {
                roleMapper.addMenu(menuParam);
            }
        }
        return true;
    }

    @Override
    public List<Menu> selectMenuList() {
        List<Menu> menus=roleMapper.selectMenuList();
        return menus;
    }

    @Override
    public Boolean userChangeRole(Integer userId, Integer roleId) {
        int i = roleMapper.userChangeRole(userId, roleId);
        return i==1;
    }

    @Override
    public boolean deleteMenu(Integer menuId) {
        Integer integer = roleMapper.deleteMenu(menuId);
        return integer==1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteRoleById(Integer roleId) {
        Integer integer = roleMapper.deleteRoleMenu(roleId);
        Integer integer1 = roleMapper.deleteRoleByRoleId(roleId);
        return integer1==1;
    }

    @Override
    public Boolean addRole(RoleParams roleParams) {

        Integer integer = roleMapper.addRole(roleParams);
        return integer==1;
    }

    @Override
    public Boolean changeMenu(Menu menu) {
        Integer aBoolean = roleMapper.changeMenu(menu);
        return aBoolean!=0;
    }

    @Override
    public Boolean changRole(Role role) {
        Integer aBoolean = roleMapper.changRole(role);
        return aBoolean!=0;
    }

    @Override
    public Boolean roleDeleteMenus(RoleMenusParams role) {
        List<Integer> menuIds = role.getMenuIds();
        int i = roleMapper.deleteRoleAllMenu(role.getRoleId(), menuIds);
        return i!=0;
    }

    @Override
    public Boolean roleAddMenus(RoleMenusParams role) {
        List<Integer> menuIds = role.getMenuIds();
        int i = roleMapper.insertRoleAllMenu(role.getRoleId(), menuIds);
        return  i>0;
    }


}
